// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin
)#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switchover (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config VCAPEN = OFF     // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled)
#pragma config PLLEN = ON       // PLL Enable (4x PLL enabled)
#pragma config STVREN = OFF     // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
#include <xc.h>#include <stdint.h>
#ifndef _XTAL_FREQ    // This definition is required to calibrate __delay_us() and __delay_ms()    
#define _XTAL_FREQ 32000000
#endif// Built-in delay routine
#ifndef __delay_us(x)   
#pragma inline(_delay)    extern void _delay(uint32_t);    // NOTE: To use the macros below, YOU must have previously defined _XTAL_FREQ    
#define __delay_us(x) _delay((uint32_t)((x)*(_XTAL_FREQ/4000000.0)))    
#define __delay_ms(x) _delay((uint32_t)((x)*(_XTAL_FREQ/4000.0)))#endif
#define     L_RST       LATCbits.LATC0      // HX8249 reset pin, active low
#define     L_STB       LATCbits.LATC2      // HX8249 standby mode, active low
#define     L_SCK       LATCbits.LATC3      // HX8249 SPI serial clock
#define     L_SDI       LATCbits.LATC5      // HX8249 SPI serial data input
#define     L_CS        LATCbits.LATC7      // HX8249 chip select, active low#define     OTP_VDD     LATCbits.LATC1      //
#define     KEY         PORTEbits.RE3       //void System_Init(void){    
OSCCON = 0b01110000;    // 32 MHz    // PORT INITIALIZATION    
ANSELE = 0b00000001;    // RE0 analog    
TRISCbits.TRISC0 = 0;   // L_RST    
LATCbits.LATC0 = 0;    
TRISCbits.TRISC1 = 0;   // OTP_VDD    
LATCbits.LATC1 = 0;    
TRISCbits.TRISC2 = 0;   // L_STB    
LATCbits.LATC2 = 0;    
TRISCbits.TRISC3 = 0;   // L_SCK    
LATCbits.LATC3 = 1;    
TRISCbits.TRISC5 = 0;   // L_SDI   
LATCbits.LATC5 = 1;    
TRISCbits.TRISC7 = 0;   // L_CS    
LATCbits.LATC7 = 0;    
TRISEbits.TRISE0 = 1;   // VCOM    
TRISEbits.TRISE1 = 0;   // red LED    
LATEbits.LATE1 = 1;    
TRISEbits.TRISE2 = 0;   // green LED    
LATEbits.LATE2 = 1;    
TRISEbits.TRISE3 = 1;   // KEY    
WPUEbits.WPUE3 = 1;    
WPUB = 0;               // PortB pull-up disable    
//WPUB = 0x08;            // DOUT pull-up    
OPTION_REGbits.nWPUEN = 0;    // ADC INITIALIZATION    
//ADCON1bits.ADCS = 6;    // FOSC/64    
//ADCON1bits.ADPREF = 0;  // VREF+ is connected to VDD    
//ADCON1bits.ADFM = 1;    // Right justified    
//ADCON0bits.CHS = 5;     // AN5(RE0)    
//ADCON0bits.ADON = 1;    // ADC is enabled    
//__delay_ms(1);
}void xTFT_SendByteHX(uint8_t command, uint8_t data){ 
uint8_t i;    
L_CS = 0;    
L_SDI = 0;    
L_SCK = 0;          // R/W = 0 for write    
//xSYS_Delay(2);    
L_SCK = 1;    
//xSYS_Delay(2);    
L_SCK = 0;          // SID = 0    
//xSYS_Delay(2);    
L_SCK = 1;    
for (i = 0; i < 6; i++) // command A5:A0    {        
        L_SDI = command & 0x20 ? 1 : 0;        
        command <<= 1;        //xSYS_Delay(1);        
        L_SCK = 0;        //xSYS_Delay(2);        
        L_SCK = 1;    }    
for (i = 0; i < 8; i++) // data D7:D0    {        
        L_SDI = data & 0x80 ? 1 : 0;        
        data <<= 1;        //xSYS_Delay(1);        
        L_SCK = 0;        //xSYS_Delay(2);        
        L_SCK = 1;    }    
L_CS = 1;}void xTFT_HX8249_Init(void){    
xTFT_SendByteHX(0x00,0x00);    
//xTFT_SendByteHX(0x02,0x73); // DCLK falling edge    
xTFT_SendByteHX(0x0D,0x07);    
xTFT_SendByteHX(0x18,0x79);    
xTFT_SendByteHX(0x19,0x95);    
xTFT_SendByteHX(0x1A,0x79);    
xTFT_SendByteHX(0x1B,0x95);    
xTFT_SendByteHX(0x1C,0x79);    
xTFT_SendByteHX(0x1D,0x95);    
xTFT_SendByteHX(0x2A,0x79);    
xTFT_SendByteHX(0x2B,0x95);    
xTFT_SendByteHX(0x15,0xDD);    
xTFT_SendByteHX(0x16,0xCD);    
xTFT_SendByteHX(0x17,0xEE);    
xTFT_SendByteHX(0x20,0x6D);    
xTFT_SendByteHX(0x22,0x30);    
xTFT_SendByteHX(0x23,0x02);    
xTFT_SendByteHX(0x24,0x10);    
xTFT_SendByteHX(0x25,0x02);    
xTFT_SendByteHX(0x00,0x01);    
xTFT_SendByteHX(0x01,0x00);    
xTFT_SendByteHX(0x02,0x01);    
xTFT_SendByteHX(0x03,0x0C);    
xTFT_SendByteHX(0x04,0x0A);    
xTFT_SendByteHX(0x05,0x34);    
xTFT_SendByteHX(0x06,0x09);    
xTFT_SendByteHX(0x07,0x13);    
xTFT_SendByteHX(0x08,0x1C);    
xTFT_SendByteHX(0x09,0x21);    
xTFT_SendByteHX(0x0A,0x24);    
xTFT_SendByteHX(0x0B,0x29);    
xTFT_SendByteHX(0x0C,0x2A);    
xTFT_SendByteHX(0x0D,0x29);    
xTFT_SendByteHX(0x0E,0x20);    
xTFT_SendByteHX(0x0F,0x12);    
xTFT_SendByteHX(0x10,0x36);    
xTFT_SendByteHX(0x11,0x0A);    
xTFT_SendByteHX(0x12,0x03);    
xTFT_SendByteHX(0x13,0x0F);    
xTFT_SendByteHX(0x14,0x00);    
xTFT_SendByteHX(0x15,0x01);    
xTFT_SendByteHX(0x16,0x0C);    
xTFT_SendByteHX(0x17,0x0A);    
xTFT_SendByteHX(0x18,0x34);    
xTFT_SendByteHX(0x19,0x09);    
xTFT_SendByteHX(0x1A,0x13);    
xTFT_SendByteHX(0x1B,0x1C);    
xTFT_SendByteHX(0x1C,0x21);    
xTFT_SendByteHX(0x1D,0x24);    
xTFT_SendByteHX(0x1E,0x29);    
xTFT_SendByteHX(0x1F,0x2A);    
xTFT_SendByteHX(0x20,0x29);    
xTFT_SendByteHX(0x21,0x20);    
xTFT_SendByteHX(0x22,0x12);    
xTFT_SendByteHX(0x23,0x36);    
xTFT_SendByteHX(0x24,0x0A);    
xTFT_SendByteHX(0x25,0x03);    
xTFT_SendByteHX(0x26,0x0F);}void AD_Conversion(void){    
uint16_t AD_result16 = 0;    
__delay_ms(100);    
ADCON0bits.GO = 1;    
while (ADCON0bits.GO);    
AD_result16 = (ADRESH<<8) + ADRESL;
}void main(void){    
System_Init();    
//__delay_ms(10+20);    
while(KEY);    
__delay_ms(200);    
OTP_VDD = 1;        // set VDDOTP to 7.5V   
__delay_ms(10+20);  // wait for VDDOTP stable    
xTFT_HX8249_Init(); // set optimized value to related registers    
__delay_ms(10);    //while(KEY);    
xTFT_SendByteHX(0x00,0x09); // select page 9    
xTFT_SendByteHX(0x02,0x66); // set WOTP[7:0]=66h, OTP program enable    
// set OTP_GROUP and set OTP_WR=1    
xTFT_SendByteHX(0x01,0x01); 
xTFT_SendByteHX(0x03,0x01); 
__delay_ms(2+20);    
xTFT_SendByteHX(0x01,0x02); 
xTFT_SendByteHX(0x03,0x01); 
__delay_ms(2+20);    
xTFT_SendByteHX(0x01,0x03); 
xTFT_SendByteHX(0x03,0x01); 
__delay_ms(5+20);    
xTFT_SendByteHX(0x01,0x04); 
xTFT_SendByteHX(0x03,0x01); 
__delay_ms(4+20);    
xTFT_SendByteHX(0x01,0x05); 
xTFT_SendByteHX(0x03,0x01); 
__delay_ms(6+20);    
xTFT_SendByteHX(0x01,0x06); 
xTFT_SendByteHX(0x03,0x01); 
__delay_ms(1+20);    
xTFT_SendByteHX(0x01,0x07); 
xTFT_SendByteHX(0x03,0x01); 
__delay_ms(3+20);    
xTFT_SendByteHX(0x01,0x08); 
xTFT_SendByteHX(0x03,0x01); 
__delay_ms(20+20);    
xTFT_SendByteHX(0x01,0x1A); 
xTFT_SendByteHX(0x03,0x01); 
__delay_ms(3+20);    
OTP_VDD = 0;        // remove VDDOTP    
xTFT_SendByteHX(0x02,0x00); // set WOTP[7:0]=00h, OTP program disable	
while(1)	{        	}}